/*
 * MainTester.cpp
 */

#include "MainTester.h"
#include "sockets/SocketServerTest.h"
#include "sockets/SocketClientTest.h"

void MainTester::testSockets(bool detailedMode) {
	ClientSocket* cs = NULL;
	try {
		ServerSocket s("4000");
		s.doBind();
		s.doListen();

		ClientSocket cc;
		cc.doConnect("127.0.0.1", "4000");

		cs = s.doAccept();

		std::string msg("net msg:)");
		if (detailedMode) Log::getInstance()->info("Envio cadena: " + msg);
		cs->doSend(msg.data(), msg.size());
		if (detailedMode) Log::getInstance()->info("Cadena enviada");

		char buffer[512];
		int reciv = cc.doReceive(buffer, 512);
		if (detailedMode) Log::getInstance()->info("recibida:" + std::string(buffer, reciv));

		cs->doClose();
		cc.doClose();
		Log::getInstance()->info("Test Sockets PASO!");
	} catch (...) {
		Log::getInstance()->error("Test Sockets NO paso");
	}
	if (cs != NULL)
		delete cs;
}

void MainTester::testSocketsWithThreads(bool detailedMode) {
	try {
	SocketServerTest server(detailedMode);
	SocketClientTest client(detailedMode);
	server.start();
	while (!server.isReady());
	client.start();
	client.join();
	server.join();
	Log::getInstance()->info("Test Sockets With Threads PASO!");
	} catch (...) {
		Log::getInstance()->error("Test Sockets With Threads NO paso");
	}
}
